package com.jsmy.softapp.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.getbase.floatingactionbutton.FloatingActionsMenu;
import com.jsmy.softapp.MyApp;
import com.jsmy.softapp.R;
import com.jsmy.softapp.adapter.DailyDialogAdapter;
import com.jsmy.softapp.adapter.FragmentAdapter;
import com.jsmy.softapp.adapter.PlaneDialogAdapter;
import com.jsmy.softapp.aroute.AR;
import com.jsmy.softapp.bean.CustomerBean;
import com.jsmy.softapp.bean.GTKJListBean;
import com.jsmy.softapp.fragment.FragmentSmanager;
import com.jsmy.softapp.fragment.KHDailyFragment;
import com.jsmy.softapp.fragment.KHPlantFragment;
import com.jsmy.softapp.net.API;
import com.jsmy.softapp.net.NetWork;
import com.jsmy.softapp.presenter.CustomerPresenter;
import com.jsmy.softapp.util.MesssageUtil;
import com.jsmy.softapp.util.ToastUtil;
import com.jsmy.softapp.view.CustomerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

@Route(path = AR.A_CUSTOMER)
public class CustomerActivity extends BaseActivity<CustomerView, CustomerPresenter> implements CustomerView {

    @BindView(R.id.img_avatar)
    CircleImageView imgAvatar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_group)
    TextView tvGroup;
    @BindView(R.id.tv_product)
    TextView tvProduct;
    @BindView(R.id.tv_stage)
    TextView tvStage;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_email)
    TextView tvEmail;
    @BindView(R.id.tv_wechat)
    TextView tvWechat;
    @BindView(R.id.tv_tecen)
    TextView tvTecen;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.m_tab)
    TabLayout mTab;
    @BindView(R.id.m_viewpage)
    ViewPager mViewpage;
    FragmentAdapter adapter;
    List<Fragment> fragments;
    List<String> list;
    @BindView(R.id.nest_view)
    NestedScrollView nestView;

    @Autowired
    public String kid;

    CustomerBean.DataBean bean;

    List<GTKJListBean.DataBean.ListBean> kjList;

    private ClipboardManager cm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_customer;
    }

    @Override
    protected CustomerPresenter createPresenter() {
        return new CustomerPresenter();
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomerActivity.this.finish();
            }
        });
        nestView.setFillViewport(true);
        fragments = new ArrayList<>();
        fragments.add(FragmentSmanager.getInstance().getFragment(FragmentSmanager.KHPLANT));
        fragments.add(FragmentSmanager.getInstance().getFragment(FragmentSmanager.KHDAILY));
        fragments.add(FragmentSmanager.getInstance().getFragment(FragmentSmanager.ZILIAO));
        list = new ArrayList<>();
        list.add("沟通计划");
        list.add("沟通日志");
        list.add("沟通资料");

        mTab.setTabMode(TabLayout.MODE_FIXED);
        adapter = new FragmentAdapter(getSupportFragmentManager(), fragments, list);
        mViewpage.setAdapter(adapter);
        mViewpage.setOffscreenPageLimit(3);
        mTab.setupWithViewPager(mViewpage);

        cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

    }

    @Override
    protected void initData() {
        presenter.getkhzlinfo(kid);
        presenter.getgtkjlist();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showLoadingData(String type, Object object) {
        switch (type) {
            case API.APP_KHZL_INFO:
                bean = ((CustomerBean) object).getData();
                setCustomerInfo();
                break;
            case API.APP_GTKJ_LIST:
                kjList = ((GTKJListBean) object).getData().getList();
                break;
            case API.APP_SAVE_NEW_GTRZ_INFO:
                ToastUtil.showShort(MyApp.getMyApp(), "保存成功！");
                daily = "";
                ((KHDailyFragment)FragmentSmanager.getInstance().getFragment(FragmentSmanager.KHDAILY)).updata();
                break;
            case API.APP_SAVE_GTPLAN_INFO:
                ToastUtil.showShort(MyApp.getMyApp(), "保存成功！");
                plan = "";
                ((KHPlantFragment)FragmentSmanager.getInstance().getFragment(FragmentSmanager.KHPLANT)).updata();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        presenter.model.disposable.clear();
        super.onDestroy();
    }

    private void setCustomerInfo() {
        NetWork.setImageGlide(this,bean.getIco(),imgAvatar);
        tvName.setText(bean.getXm());
        tvGroup.setText(bean.getGroupmc());
        tvProduct.setText(bean.getPmmc());
        tvStage.setText(bean.getJd());
        tvPhone.setText(bean.getDh());
        tvEmail.setText(bean.getEmail());
        tvWechat.setText(bean.getWeixin());
        tvTecen.setText(bean.getQq());
        tvTip.setText(bean.getNote());
    }

    @OnClick({R.id.btn_plane, R.id.btn_daily,R.id.tv_phone,R.id.tv_email,R.id.tv_tecen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_plane:
                showPlaneDialog();
                break;
            case R.id.btn_daily:
                showDailyDialog();
                break;
            case R.id.tv_phone:
                MesssageUtil.call(this,bean.getDh());
                break;
            case R.id.tv_email:
                ClipData clipEmail = ClipData.newPlainText("邮箱", bean.getEmail());
                cm.setPrimaryClip(clipEmail);
                ToastUtil.showShort(this, "邮箱复制成功！");
                break;
            case R.id.tv_tecen:
                ClipData clipQQ = ClipData.newPlainText("QQ", bean.getEmail());
                cm.setPrimaryClip(clipQQ);
                ToastUtil.showShort(this, "QQ复制成功！");
                break;
            default:
                break;
        }
    }

    String plan = "";
    public void showPlaneDialog() {
        final BottomSheetDialog dialog = new BottomSheetDialog(this);
        dialog.setContentView(R.layout.bottom_sheet_plane);
        dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);
        final TextView btnMsg = (TextView) dialog.findViewById(R.id.btn_msg);
        final EditText edtMsg = (EditText) dialog.findViewById(R.id.edt_msg);
        edtMsg.setText(plan);
        edtMsg.setSelection(plan.length());
        final TextView btnSave = (TextView) dialog.findViewById(R.id.btn_save);
        final RecyclerView mRcycler = (RecyclerView) dialog.findViewById(R.id.m_rcycler);
        PlaneDialogAdapter adapter = new PlaneDialogAdapter();
        mRcycler.setLayoutManager(new GridLayoutManager(this, 7));
        mRcycler.setAdapter(adapter);
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= 31; i++) {
            list.add(i + "");
        }
        final List<String> items = list;
        adapter.replaceData(items);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                btnMsg.setText(items.get(position));
            }
        });
        edtMsg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                plan = s.toString();
            }
        });
        btnMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRcycler.getVisibility() == View.VISIBLE) {
                    mRcycler.setVisibility(View.GONE);
                } else {
                    mRcycler.setVisibility(View.VISIBLE);
                }
            }
        });
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!edtMsg.getText().toString().isEmpty())
                    presenter.savegtpalninf(kid, edtMsg.getText().toString(), btnMsg.getText().toString());
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    String daily = "";
    public void showDailyDialog() {
        final BottomSheetDialog dialog = new BottomSheetDialog(this);
        dialog.setContentView(R.layout.bottom_sheet_daily);
        dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);
        final ImageView btnMsg = (ImageView) dialog.findViewById(R.id.btn_msg);
        final EditText edtMsg = (EditText) dialog.findViewById(R.id.edt_msg);
        edtMsg.setText(daily);
        edtMsg.setSelection(daily.length());
        final TextView btnSave = (TextView) dialog.findViewById(R.id.btn_save);
        final RecyclerView mRcycler = (RecyclerView) dialog.findViewById(R.id.m_rcycler);
        DailyDialogAdapter adapter = new DailyDialogAdapter();
        mRcycler.setLayoutManager(new LinearLayoutManager(this));
        mRcycler.setAdapter(adapter);
        adapter.replaceData(kjList);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                edtMsg.setText(kjList.get(position).getMc());
            }
        });
        edtMsg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                daily = s.toString();
            }
        });
        btnMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRcycler.getVisibility() == View.VISIBLE) {
                    mRcycler.setVisibility(View.GONE);
                    btnMsg.setImageResource(R.drawable.ic_menu_arrowupward);
                } else {
                    mRcycler.setVisibility(View.VISIBLE);
                    btnMsg.setImageResource(R.drawable.ic_menu_arrowdownward);
                }
            }
        });
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!edtMsg.getText().toString().isEmpty())
                    presenter.savenewgtrzinfo(kid, edtMsg.getText().toString(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ((KHPlantFragment)FragmentSmanager.getInstance().getFragment(FragmentSmanager.KHPLANT)).updata();
    }

}
